package com.cyj.dream.admin.controller.admin.system;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cyj.dream.admin.model.request.SysRoleQueryRequest;
import com.cyj.dream.admin.model.system.SysRole;
import com.cyj.dream.admin.service.system.SysRoleService;
import com.cyj.dream.core.aspect.annotation.ResponseResult;
import com.cyj.dream.core.aspect.annotation.log.LogRecord;
import com.cyj.dream.core.aspect.enums.OperatorType;
import com.cyj.dream.core.constant.Constant;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.constant.pagemodel.PageParam;
import com.cyj.dream.core.exception.BizException;
import com.cyj.dream.core.util.IsEmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 系统角色模块控制器
 * @BelongsProject: fckj-restaurant
 * @BelongsPackage: com.fckj.fckjrestaurant.controller.admin.systems
 * @Author: ChenYongJia
 * @CreateTime: 2021-04-21 10:34
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@RestController
@ResponseResult
@Api(value = "role", tags = "角色模块")
@RequestMapping(value = "/pc/role", name = "角色模块")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 分页查询角色信息
     *
     * @param pageParam 系统角色查询request
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/4/21 10:44
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "分页查询角色信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageParam", value = "系统角色查询request", dataType = "PageParam<SysRoleQueryRequest>", required = true)
    })
    @LogRecord(title = "分页查询角色信息", operatetype = OperatorType.SELECT)
    @RequestMapping(value = "/selectSysRole", name = "分页查询角色信息", method = RequestMethod.POST)
    public Map<String, Object> selectSysRole(@RequestBody PageParam<SysRoleQueryRequest> pageParam) {
        log.info("进入分页查询角色信息方法控制器，参数为 pageParam：{}", JSONObject.toJSONString(pageParam));
        if (IsEmptyUtils.isEmpty(pageParam.getPageNum()) || IsEmptyUtils.isEmpty(pageParam.getPageSize())) {
            log.error("出现错误：分页参数缺失查询失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        IPage<SysRole> sysRoleList = sysRoleService.list(pageParam);
        map.put("list", sysRoleList);
        return map;
    }

    /**
     * 添加角色详情信息
     *
     * @param sysRole 用户信息
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/4/21 10:44
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "添加角色信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRole", value = "角色对象", dataType = "SysRole", required = true)
    })
    @LogRecord(title = "添加角色信息", operatetype = OperatorType.INSERT)
    @RequestMapping(value = "/addRoleInfo", name = "添加角色信息", method = RequestMethod.POST)
    public Map<String, Object> addRoleInfo(@RequestBody SysRole sysRole) {
        log.info("进入添加用户详情信息方法控制器，参数为 sysRole：{}", JSONObject.toJSONString(sysRole));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysRole.getSysRoleName()) || IsEmptyUtils.isEmpty(sysRole.getSysRoleRemark())) {
            log.error("出现错误：参数缺失添加角色详情信息失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        map.put("info", sysRoleService.addRoleInfo(sysRole));
        return map;
    }

    /**
     * 修改角色信息
     *
     * @param sysRole 角色信息
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/4/21 10:49
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "修改角色信息", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRole", value = "角色对象", dataType = "SysRole", required = true)
    })
    @LogRecord(title = "修改角色信息", operatetype = OperatorType.UPDATE)
    @RequestMapping(value = "/updRoleInfo", name = "修改角色信息", method = RequestMethod.PUT)
    public Map<String, Object> updRoleInfo(@RequestBody SysRole sysRole) {
        log.info("进入修改角色详情信息方法控制器，参数为 sysRole：{}", JSONObject.toJSONString(sysRole));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysRole.getDreamId())) {
            log.error("出现错误：参数缺失修改角色详情信息失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }
        map.put("info", sysRoleService.updRoleInfo(sysRole));
        return map;
    }

    /**
     * 删除角色信息
     *
     * @param sysRole 角色信息
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/4/21 10:45
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "删除角色信息", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRole", value = "角色对象", dataType = "List<SysRole>", required = true)
    })
    @LogRecord(title = "删除角色信息", operatetype = OperatorType.DELETE)
    @RequestMapping(value = "/delRoleInfo", name = "删除角色信息", method = RequestMethod.DELETE)
    public Map<String, Object> delRoleInfo(@RequestBody List<SysRole> sysRole) {// 有待修订
        log.info("进入删除角色信息方法控制器，参数为 sysRole：{}", JSONObject.toJSONString(sysRole));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysRole)) {
            log.error("出现错误：参数缺失删除角色信息失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        map.put("info", sysRoleService.delRoleInfo(sysRole));
        return map;
    }

    /**
     * 根据角色id查询角色信息
     *
     * @param sysRoleId 角色id
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021-5-19 14:24:37
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "根据角色id查询角色信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRoleId", value = "角色id", dataType = "Long", required = true)
    })
    @LogRecord(title = "根据角色id查询角色信息", operatetype = OperatorType.SELECT)
    @RequestMapping(value = "/findBySysRoleId", name = "根据角色id查询角色信息", method = RequestMethod.GET)
    public Map<String, Object> findBySysRoleId(Long sysRoleId) {
        log.info("进入根据角色id查询角色信息方法控制器，参数为 sysRoleId：{}", sysRoleId);
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysRoleId)) {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        SysRole sysRole = new SysRole();
        sysRole.setDreamId(sysRoleId);
        map.put("info", sysRoleService.selectBySysRoleIdOrSysRoleTrueName(sysRole));
        return map;
    }

}
